package com.controller;

import com.model.AuthGroup;
import com.model.AuthRule;
import com.model.Manager;
import com.model.Modules;
import com.service.AuthGroupService;
import com.service.AuthRuleService;
import com.service.ManagerService;
import com.service.ModulesService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: AuthorityController
 * @Description: 权限管理控制层
 * @author 陈靖
 * @date 2017-1-17 上午11:48:57
 */
@Controller("AuthorityController")
public class AuthorityController {
	@Resource
	private AuthGroupService authGroupService;
	@Resource
	private ModulesService modulesService;
	@Resource
	private AuthRuleService authRuleService;
	@Resource
	private ManagerService managerService;

	@RequestMapping(value = "authGroupList.do", method = RequestMethod.GET)
	public String authGroupList(HttpServletRequest request) {
		List<AuthGroup> authGroups = authGroupService.getAuthGroupList();
		List<Manager> managers = managerService.getManagerList();
		List<Manager> list = new ArrayList<Manager>();
		for (AuthGroup authGroup : authGroups) {
			for (Manager manager : managers) {
				if (manager.getGroup_id() == authGroup.getId().intValue()) {
					list.add(manager);
					authGroup.setList(list);
				}
			}
		}
		request.setAttribute("authGroups", authGroups);
		return "authorityGroup";
	}

	@RequestMapping(value = "insertAuthGroup.do", method = RequestMethod.POST)
	public String insertAuthGroup(HttpServletRequest request,
			@RequestParam(value = "title", required = true) String title,
			@RequestParam(value = "describe", required = true) String discribe) {
		List<AuthGroup> authGroups = authGroupService.getAuthGroupList();
		for (AuthGroup authGroup : authGroups) {
			if (authGroup.getTitle().equals(title)) {
				request.setAttribute("errormessage", "角色名称已存在");
				return authGroupList(request);
			}
		}
		authGroupService.insertAuthGroup(new AuthGroup(title, discribe));
		return authGroupList(request);
	}

	@RequestMapping(value = "updateAuthGroup.do", method = RequestMethod.POST)
	public String updateAuthGroup(HttpServletRequest request,
			@RequestParam(value = "rules", required = true) String rules,
			@RequestParam(value = "id", required = true) Integer id,
			@RequestParam(value = "status", required = true) Integer status) {
		authGroupService.modifyAuthGroupRules(new AuthGroup(id, rules, status));
		return authGroupList(request);
	}

	@RequestMapping(value = "deleteAuthGroup.do", method = RequestMethod.GET)
	public String deleteAuthGroup(HttpServletRequest request,
			@RequestParam(value = "id", required = true) Integer id) {
		authGroupService.deleteAuthGroup(new AuthGroup(id));
		return authGroupList(request);
	}

	@RequestMapping(value = "modulesList.do", method = RequestMethod.GET)
	public String modulesList(HttpServletRequest request) {
		List<Modules> modules = modulesService.getModulesList();
		request.setAttribute("modules", modules);
		return "modulesList";
	}

	@RequestMapping(value = "insertModule.do", method = RequestMethod.POST)
	public String insertModule(
			HttpServletRequest request,
			@RequestParam(value = "moduleName", required = true) String moduleName) {
		List<Modules> modules = modulesService.getModulesList();
		for (Modules module : modules) {
			if (module.getModuleName().equals(moduleName)) {
				request.setAttribute("errormessage", "模块名已存在");
				return modulesList(request);
			}
		}
		modulesService.insertModules(new Modules(moduleName));
		return modulesList(request);
	}

	@RequestMapping(value = "modifyModule.do", method = RequestMethod.POST)
	public String modifyModule(
			HttpServletRequest request,
			@RequestParam(value = "id", required = true) Integer id,
			@RequestParam(value = "moduleName", required = true) String moduleName) {
		List<Modules> modules = modulesService.getModulesList();
		for (Modules module : modules) {
			if (module.getModuleName().equals(moduleName)
					&& module.getId() != id) {
				request.setAttribute("errormessage", "模块名已存在");
				return modulesList(request);
			}
		}
		modulesService.modifyModules(new Modules(id, moduleName));
		return modulesList(request);
	}

	@RequestMapping(value = "deleteModule.do", method = RequestMethod.GET)
	public String deleteModule(HttpServletRequest request,
			@RequestParam(value = "id", required = true) Integer id) {
		modulesService.deleteModules(new Modules(id));
		return modulesList(request);
	}

	@RequestMapping(value = "authRuleList.do", method = RequestMethod.GET)
	public String authRuleList(HttpServletRequest request,
			@RequestParam(value = "mid", required = true) Integer mid) {
		List<AuthRule> authRules = authRuleService
				.getAuthRuleList(new AuthRule(mid, null));
		List<Modules> modules = modulesService.getModulesList();
		request.setAttribute("modules", modules);
		request.setAttribute("authRules", authRules);
		return "authRuleList";
	}

	@RequestMapping(value = "insertauthRule.do", method = RequestMethod.POST)
	public String insertauthRule(
			HttpServletRequest request,
			@RequestParam(value = "name", required = true) String name,
			@RequestParam(value = "title", required = true) String title,
			@RequestParam(value = "condition", required = false, defaultValue = "") String condition,
			@RequestParam(value = "mid", required = true) Integer mid) {
		List<AuthRule> authRules = authRuleService.getAuthRuleMessageList();
		System.out.println("title" + title);
		for (AuthRule authRule : authRules) {
			if (authRule.getName().equals(name)) {
				request.setAttribute("errormessage", "规则标识已存在");
				return authRuleList(request, mid);
			}
			/*
			 * if (authRule.getTitle().equals(title)) {
			 * request.setAttribute("errormessage", "规则名称已存在"); return
			 * authRuleList(request, mid); }
			 */
		}

		authRuleService
				.insertAuthRule(new AuthRule(name, title, condition, mid));
		return authRuleList(request, mid);
	}

	@RequestMapping(value = "deleteAuthRule.do", method = RequestMethod.GET)
	public String deleteAuthRule(HttpServletRequest request,
			@RequestParam(value = "id", required = true) Integer id,
			@RequestParam(value = "mid", required = true) Integer mid) {
		authRuleService.deleteAuthRule(new AuthRule(id));
		return authRuleList(request, mid);
	}

	@RequestMapping(value = "getAuthRuleList.do", method = RequestMethod.GET)
	public String getAuthRuleList(HttpServletRequest request,
			@RequestParam(value = "id", required = true) Integer id,
			@RequestParam(value = "rules", required = true) String rules) {
		List<AuthRule> authRules = authRuleService.getAuthRuleMessageList();
		List<Modules> modules = modulesService.getAdminModulesList();
		for (Modules module : modules) {
			List<AuthRule> list = new ArrayList<AuthRule>();
			for (AuthRule authRule : authRules) {
				if (module.getId().intValue() == authRule.getMid().intValue()) {
					list.add(authRule);
					module.setAuthRules(list);
				}
			}
		}
		request.setAttribute("modules", modules);
		request.setAttribute("rules", rules);
		request.setAttribute("id", id);
		return "newAuthRuleList";
	}

	@RequestMapping(value = "setAuthRule.do", method = RequestMethod.GET)
	public String setAuthRule(HttpServletRequest request,
			@RequestParam(value = "id", required = true) Integer id,
			@RequestParam(value = "rules", required = true) String rules) {
		authGroupService.modifyAuthGroupRules(new AuthGroup(id, rules));
		return getAuthRuleList(request, id, rules);
	}

}
